package com.mycompony.shopping.shangguan.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mycompony.shopping.shangguan.sys.dao.UserRoleMapper;
import com.mycompony.shopping.shangguan.sys.model.Role;
import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.service.UserRoleService;

public class UserRoleServiceImpl implements UserRoleService {

	private UserRoleMapper userRoleMapper;

	public Map<String, Role> getUserAllRole(User user) {

		Map<String, Role> map = new HashMap<String, Role>();

		List<Role> userRoleList = getUserRole(user);

		List<Role> organizationRoleList = getOrganizationRole(user);

		if (userRoleList != null && !userRoleList.isEmpty()) {
			for (Role role : userRoleList) {
				if (role == null || StringUtils.isEmpty(role.getRoleid())) {
					break;
				}
				map.put(role.getRoleid(), role);
			}
		}

		if (organizationRoleList != null && !organizationRoleList.isEmpty()) {
			for (Role role : organizationRoleList) {
				if (role == null || StringUtils.isEmpty(role.getRoleid())) {
					break;
				}
				map.put(role.getRoleid(), role);
			}
		}
		return map;
	}

	public List<Role> getOrganizationRole(User user) {

		if (user == null || StringUtils.isEmpty(user.getOrganizationid())) {
			return null;
		}
		return userRoleMapper.getOrganizationRole(user);
	}

	public List<Role> getUserRole(User user) {

		if (user == null || StringUtils.isEmpty(user.getUserid())) {
			return null;
		}
		return userRoleMapper.getUserRole(user);
	}

	public UserRoleMapper getUserRoleMapper() {
		return userRoleMapper;
	}

	public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
		this.userRoleMapper = userRoleMapper;
	}

}
